package com.ddx.common.util;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/5/23
 */
public class DesUtil {
    private static Logger logger = LoggerFactory.getLogger(DesUtil.class);
    /**
     * des加密key
     */
    private static String desSKey="asdt.14k";
    /**
     * rsa公钥
     */
    private static String publickey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXHZwtv5jcBaAlsGLIFFIanmlGhvPzI/3u8DO9FX7lqU84fZPWls0n+3wPIBqUcpin+dAyGGHkf5jGeykUwcDB6mGyfxLTopfT8fGP8Vt8oyeJ4tlnUHneXUDKm7WiUu7mPsTxMifj5PrN/m5om09cZDpAlXtYZbNM6rBEaBvLBwIDAQAB";
    /**
     * rsa私钥
     */
    private static String privatekey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANcdnC2/mNwFoCWwYsgUUhqeaUaG8/Mj/e7wM70VfuWpTzh9k9aWzSf7fA8gGpRymKf50DIYYeR/mMZ7KRTBwMHqYbJ/EtOil9Px8Y/xW3yjJ4ni2WdQed5dQMqbtaJS7uY+xPEyJ+Pk+s3+bmibT1xkOkCVe1hls0zqsERoG8sHAgMBAAECgYBq2pB7wRKDKGA2mHlY/Y8THeEZYWvIOPaoO7TXldfk6McrPmWzUFE0LR2gvce2OX5O4i5i0+28fQ08em0bnGrMfyc7dFVPtnJlALqH1xNkALB4XHxwEbkO0nuQz7gliaaqGPILHfuZtFHcXJ8RoYN2m9+cPe0VFVEX7gTpcw8ecQJBAOtQ+SOTa4Tx1g6OpFw2hVT04gDKmfWErxsgsWW1fU7/H1dqMxAi+tDxmZYsy53P3E4g0zwiiskor0mzIbyVPYMCQQDqBhZb2nhFI81T9A0V8detKHjBWobYwT9V7Gw6OUoKPeoK4dvnvgs5SFib0PQiBmFYBzaq2mxNDV278IKgAiktAkBFbmHkXlYGJvj62JmkX63dkFey1hSxPZIRFgdhtDgh++K6fypL45ZP2klv5w25JaCiKMtK3gJHMkruGv2kUACTAkEAgXccp3QVVSqZTPiWxK0mRAU/yLfsIb3G7CH/Pbaz3zahY6PPpETesLH1Uz4l0Sz2bVyQQbTUdUwPrwk/OoX3gQJBAK6soyKaI9IWWDR6OCN4lpHne+ZiH1hPtP/+vgnNGNW9gYjM6YlqrDkk9uwJZFccnZct0T+OQhEKWOAVLmXYO1Q=";

    /**
     * 加密
     * @param srcStr
     * @param sKey
     * @return
     */
    public static String encrypt(String srcStr, String sKey) {
        byte[] src = srcStr.getBytes(Charset.forName("ISO-8859-1"));
        byte[] buf = DES.encrypt(src, sKey);
        return DES.parseByte2HexStr(buf);
    }

    /**
     * 解密
     *
     * @param hexStr
     * @return
     * @throws Exception
     */
    public static String decrypt(String hexStr, String sKey) throws Exception {
        byte[] src = DES.parseHexStr2Byte(hexStr);
        byte[] buf = DES.decrypt(src, sKey);
        return new String(buf, Charset.forName("ISO-8859-1"));
    }
    /**
     * 压缩字符串
     *
     * @param str
     *            压缩的字符串
     * @return 压缩后的字符串
     */
    public static String compress(String str) {

        if (StringUtils.isEmpty(str)) {
            return str;
        }

        try {
            ByteArrayOutputStream bos = null;
            GZIPOutputStream os = null; // 使用默认缓冲区大小创建新的输出流
            byte[] bs = null;
            try {
                bos = new ByteArrayOutputStream();
                os = new GZIPOutputStream(bos);
                os.write(str.getBytes()); // 写入输出流
                os.close();
                bos.close();
                bs = bos.toByteArray();
                return new String(bs, "ISO-8859-1"); // 通过解码字节将缓冲区内容转换为字符串
            } finally {
                bs = null;
                bos = null;
                os = null;
            }
        } catch (Exception ex) {
            return str;
        }
    }

    /**
     * 解压缩字符串
     *
     * @param str
     *            解压缩的字符串
     * @return 解压后的字符串
     */
    public static String decompress(String str) {

        if (StringUtils.isEmpty(str)) {
            return str;
        }

        ByteArrayInputStream bis = null;
        ByteArrayOutputStream bos = null;
        GZIPInputStream is = null;
        byte[] buf = null;
        try {
            bis = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
            bos = new ByteArrayOutputStream();
            is = new GZIPInputStream(bis); // 使用默认缓冲区大小创建新的输入流
            buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) { // 将未压缩数据读入字节数组
                // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此byte数组输出流
                bos.write(buf, 0, len);
            }
            is.close();
            bis.close();
            bos.close();
            return new String(bos.toByteArray()); // 通过解码字节将缓冲区内容转换为字符串
        } catch (Exception ex) {
            return str;
        } finally {
            bis = null;
            bos = null;
            is = null;
            buf = null;
        }
    }
}
